package com.uenpay.oem.ui.trans.receivable;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.utilities.DensityUtils;
import com.uenpay.im.utilities.Toasts;
import com.uenpay.oem.R;
import com.uenpay.util.app.base.BaseActivity;
import com.uenpay.util.common.PicUtil;
import com.uenpay.util.imageloader.ImageLoader;
import com.uenpay.util.imageloader.ImageLoaderUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: liulou
 * Date: 2017/6/23.
 * desc: 二维码收款
 */

public class QRCodeCollectionActivity extends BaseActivity implements QRCodeCollectionContract.View {

    private static final String TAG = "QRCodeCollectionActivit";
    @BindView(R.id.iv_qrcode_icon)
    ImageView ivQrcodeIcon;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrcode;
    @BindView(R.id.iv_qr_code_avatar)
    ImageView ivQrcodeAvatar;
    @BindView(R.id.tv_amount_settings)
    TextView tvAmountSettings;
    @BindView(R.id.tv_save_qrcode)
    TextView tvSaveQrcode;
    @BindView(R.id.iv_user_avatar)
    ImageView ivUserAvatar;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.btn_receivable_tradebook)
    Button btnReceivableTradebook;
    @BindView(R.id.ll_qrcode_operate)
    LinearLayout llQrcodeOperate;
    @BindView(R.id.cv_qrcode)
    CardView cvQrCode;

    QRCodeCollectionContract.Presenter mPresenter;

    //是否设置了金额
    private boolean isSettingAmount = false;

    private Bitmap mQrCodeBitmap;

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activty_qrcode_collection;
    }

    @Override
    public void initView(View view) {

        setPresenter(null);
        setTitle(R.string.title_receivable);
        String url = mPresenter.generateUrl("");
        KLog.d(TAG, "[initView] url=" + url);
        mPresenter.getQrCode(url, getResources().getDimensionPixelSize(R.dimen.paycode_qrcode_size), true);
        String avatarUrl = APIConfiguration.getAvatarUrlNormal(IMApplication.getCustId(), PacketType.PERSONAL);
        ImageLoaderUtil.getInstance().loadImage(this, new ImageLoader.Builder().url(avatarUrl).roundRadius(6).imgView(ivQrcodeAvatar).build());

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void setPresenter(Object presenter) {
        mPresenter = new QRCodeCollectionPresenter(this);
    }

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showQrCode(Bitmap bitmap) {
        mQrCodeBitmap = bitmap;
        ivQrcode.setImageBitmap(bitmap);
    }

    @OnClick({R.id.tv_amount_settings, R.id.tv_save_qrcode, R.id.btn_receivable_tradebook})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_amount_settings:
                settingAmount();
                break;
            case R.id.tv_save_qrcode:
                saveQrCode();
                break;
            case R.id.btn_receivable_tradebook:
                break;
        }
    }

    private void settingAmount() {
        if (isSettingAmount) {
            tvAmount.setVisibility(View.GONE);
            tvMsg.setVisibility(View.GONE);
            tvAmountSettings.setText("设置金额");
            isSettingAmount = false;
            String url = mPresenter.generateUrl("");
            mPresenter.getQrCode(url, DensityUtils.dip2px(180), true);
        } else {
            Intent it = new Intent(QRCodeCollectionActivity.this, InputAmountActivity.class);
            startActivityForResult(it, 100);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            String amount = data.getStringExtra("amount");
            String msg = data.getStringExtra("msg");
            setAmountAndMsg(amount, msg);
            String url = mPresenter.generateUrl(amount);
            mPresenter.getQrCode(url, getResources().getDimensionPixelSize(R.dimen.paycode_qrcode_size), false);

        }
    }

    /**
     * 设置或清除金额
     *
     * @param amount
     * @param msg
     */
    private void setAmountAndMsg(String amount, String msg) {
        tvAmount.setText("¥ " + amount);
        tvAmount.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(msg)) {
            tvMsg.setText(msg);
            tvMsg.setVisibility(View.VISIBLE);
        }
        tvAmountSettings.setText("清除金额");
        isSettingAmount = true;
    }

    /**
     * 保存收款码图片
     */
    private void saveQrCode() {
        llQrcodeOperate.setVisibility(View.GONE);
        Bitmap bitmap = PicUtil.convertViewToBitmap(cvQrCode);
        String path = PicUtil.saveImageToGallery(this, bitmap);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        llQrcodeOperate.setVisibility(View.VISIBLE);
        Toasts.longToast(this, String.format(getString(R.string.msg_save_qrcode_success), path));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!mQrCodeBitmap.isRecycled()) {
            mQrCodeBitmap.recycle();
        }
    }
}
